import { keyData, foreignData } from './mysql-data'
const { $fn, $, $http } = window
const Index = ({ database, table_name, tableRef}) => {
	const config = {
		modal:{
			title 	: <span>【<b className='c3 ar'>{table_name}</b>】添加外键</span>,
			width 	: '40%',
		},
		form: {
			labelWidth: 's5',
			api: 'mysql/structure/add_foreign_key',
			way:'put',
			infoApi : false,
			param: { database, table_name },
			data: [
				{ label: '外键名称', name: 'foreign_name' },
				{ label: '从表字段', name: 'Field', type:'select', data:keyData, listHeight:600, r:1, mul:1},
				{ label: '主表名称', name: 'tab_name', type:'select', api:'mysql/table_dict', param:{ database }, idStr:'label', listHeight:600, r:1, change:({ ref, value })=>{
					if( value === table_name ){
						$fn.msg.warning('不能选择自身关键关外键')
						setTimeout(()=>ref.setValue({ tab_field:null }))
					}else{
						$http.pull(null,'mysql/col', { param:{ database, table_name: value }, loading:true }).then(d=>{
							$fn.getRow(config, 'tab_field').data = d
							ref.setValue({ tab_field:d[0].col })
							ref.update()
						})
					}
				}},
				{ label: '主表字段', name: 'tab_field', type:'select', idStr:'col', nameStr:'col', listHeight:600, r:1 },
				{ label: '级联操作', name: 'extra', type:'select', group:1, data:foreignData, listHeight:600 },
			],
			onOk : () => {
				$.ref(tableRef).tableRef().search({ scroll:0 })
			}
		},
		onEnd: ({ update, tableRef }) => {
			$fn.getRow(config, 'Field').data = tableRef.data().map( v => v.Field )
			update()
		}
	}
	return config
}

export default Index